export default {
  data () {
    return {
      currentPage: 1,
      limit: 12
    }
  },
  methods: {
    fetch (page = false, params) {
      const { query, limit, checkForm, total } = this
      if (page) this.currentPage = page
      if (page > 1 && (page - 1) * limit >= total) return this.fetch(Math.ceil(total / limit))
      const { time_start = false, start_time = false, time_end = false, end_time = false, ...r } = query
      if (time_start) {
        r.time_start = time_start / 1000
      }
      if (start_time) {
        r.start_time = start_time / 1000
      }
      if (time_end) {
        r.time_end = time_end / 1000
      }
      if (end_time) {
        r.end_time = end_time / 1000
      }

      const newForm = {}
      Object.keys(r).filter(ele => checkForm(ele)).forEach((ele) => { newForm[ele] = r[ele] })
      return this.fetchList({ ...newForm, page: this.currentPage, limit })
    },
    checkForm (ele) {
      const { query: form } = this
      const isBoolean = typeof form[ele] === 'boolean'
      const isNotEmptyString = form[ele] && form[ele].length >= 1
      const isNumber = typeof form[ele] === 'number' && !Number.isNaN(form[ele])
      return isBoolean || isNotEmptyString || isNumber
    }
  }
}
